$(function () {
    htmbdy_init();
});

function htmbdy_init() {

    // 合同定义加载
    $("[name='htmbdy_htdyid']").htdycombobox(false, '');

    var options = gethtmbdyGridOption();
    $('#htmbdy_grid').datagrid(options);
}

function gethtmbdyGridOption() {

    var columns = [
        [
            {field: 'htmbmc', title: '合同模板名称', width: 30},
            {field: 'mc', title: '合同名称', width: 30},
            {field: 'htmbxx', title: '合同模板详细', width: 30},
            {field: 'bz', title: '备注', width: 40},
            {field: 'date_created', title: '新增时间', width: 40},
            {field: 'qc', title: '公司或部门名称', width: 40}
        ]
    ];

    var options = {
        url: $.webPath() + "/htdygl/htmb/htmbList",
        toolbar: "#htmbdy_toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        columns: columns,
        rownumbers: true,
        onClickRow: function (rowIndex, rowData) {
            if (rowData != null && rowData.id != null) {
                $("#htmbdy_mod").show();
                $("#htmbdy_del").show();
            }
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                this.htmbxx = '<a href="javascript:void(0);" onclick="checkDetail(' + this.id + ')" style="color: red">' + "查看详细" + '</a>';
            })
            return data;
        }
    };
    return options;
}

//*************按钮触发事件***********
// 查看详细html
function checkDetail(htmbId) {
    var href = $.webPath() + "/htdygl/htmb/htmbdyDetail?htmbId=" + htmbId
    showDetailHtmbDialog("合同模板详细信息", href);
}

// 弹出详细页面
function showDetailHtmbDialog(title, href) {
    var win = $.window({
        title: title,
        width: 1000,
        height: 550,
        cache: false,
        url: href,
        modal: true
    });
}

// 查询
function htmbdy_search() {
    var htdyid = $("#htmbdy_toolbar input[name='htmbdy_htdyid']").val();
    var htmbmc = $("#htmbdy_toolbar input[name='htmb_mc']").val();
    var options = $.extend(gethtmbdyGridOption(),
        {queryParams: {htdyId: htdyid, htmbmc: htmbmc}});
    $('#htmbdy_grid').datagrid(options);
}

// 新增合同模板
$("#htmbdy_add").click(function () {
    var href = $.webPath() + "/htdygl/htmb/htmbdy_content";
    var actionUrl = $.webPath() + "/htdygl/htmb/addOrUpdateHtmb";
    showHtmbDialog("新增合同模板", href, actionUrl);
});


// 双击事件
$("#htmbdy_grid").datagrid({
    onDblClickCell: function (index, field, value) {
        var record = $("#htmbdy_grid").datagrid("getSelected");
        if (record == null) {
            $.messager.alert("提示", "请选择要更新的记录!", "info");
            return;
        }
        var href = $.webPath() + "/htdygl/htmb/htmbdy_content?htmbId=" + record.id;
        var actionUrl = $.webPath() + "/htdygl/htmb/addOrUpdateHtmb?htmbId=" + record.id;
        showHtmbDialog("修改合同模板", href, actionUrl);
    }
});

// 修改合同模板定义
$("#htmbdy_mod").click(function () {
    var record = $("#htmbdy_grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "info");
        return;
    }
    var href = $.webPath() + "/htdygl/htmb/htmbdy_content?htmbId=" + record.id;
    var actionUrl = $.webPath() + "/htdygl/htmb/addOrUpdateHtmb?htmbId=" + record.id;
    showHtmbDialog("修改合同模板", href, actionUrl);
});

// 删除合同文本
$("#htmbdy_del").click(function () {
    var record = $("#htmbdy_grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要删除的记录!", "info");
        return;
    }
    $.messager.confirm("提示", "是否确认删除该合同模板?", function (r) {
        if (r) {
            $.ajax({
                type: "POST",
                url: $.webPath() + "/htdygl/htmb/deleteHtmb",
                dataType: "json",
                data: {htmbId: record.id},
                beforeSend: function () {
                    $.messager.progress();
                },
                success: function (data) {
                    $.messager.progress("close");
                    var icon = "info";
                    if (data.statusCode == 300)
                        icon = "error";
                    $.messager.alert("提示", data.message, icon);
                    if (data.statusCode == 200)
                        loadhtmbdy();
                }
            });
        }
    });
});

// 弹出新增、修改的窗口
function showHtmbDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 900,
        height: 600,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $.messager.progress();
                    $("#htmbForm").form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $.messager.progress('close');
                            }
                            return isValid;
                        },
                        success: function (data) {
                            if (null != $("[name='htmb_id']").val() && $("[name='htmb_id']").val() != '') {  //如果有模块ID，要保存模块内容
                                htmb_save();
                            }
                            $.messager.progress('close');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                if (null != $("[name='htmb_id']").val() && $("[name='htmb_id']").val() != '') {  //如果有模块ID，要保存模块内容
                                    $.messager.show({title: '提示框', msg: json.message, timeout: 3000, showType: 'slide'});
                                    $(win).window('close');
                                    $('#htmbdy_grid').datagrid('reload');
                                }
                                else {
                                    $.messager.confirm('确认', '是否编辑合同模板', function (r) {
                                        if (r) {
                                            $(win).window('close');
                                            $('#htmbdy_grid').datagrid('reload');
                                            $.messager.show({title: '提示框', msg: json.message, timeout: 3000, showType: 'slide'});
                                            var href = $.webPath() + "/htdygl/htmb/htmbdy_content?htmbId=" + json.id;
                                            var actionUrl = $.webPath() + "/htdygl/htmb/addOrUpdateHtmb?htmbId=" + json.id;
                                            showHtmbDialog("修改合同模板", href, actionUrl);
                                        } else {
                                            $(win).window('close');
                                            $('#htmbdy_grid').datagrid('reload');
                                            $.messager.show({title: '提示框', msg: json.message, timeout: 3000, showType: 'slide'});
                                        }
                                    });
                                }
                            } else {
                                $.messager.alert('提示', json.message, 'error');
                            }
                        }
                    });
                }
            }
        ]
    });
}

//重新加载页面
function loadhtmbdy() {
    var htdyid = $("#htmbdy_toolbar input[name='htmbdy_htdyid']").val();
    var htmbmc = $("#htmbdy_toolbar input[name='htmb_mc']").val();
    $('#htmbdy_grid').datagrid('reload', {
        htdyId: htdyid,
        htmbmc: htmbmc
    });
}

